package com.qeeyou.apps.android.craftsman.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.po.AreaInfo;
import com.qeeyou.apps.android.craftsman.po.UserAddressInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuchengxiong on 2017/12/17.
 */

public class AddressSelectAdapter extends BaseAdapter {

    private Activity mActivity;
    private List<UserAddressInfo> addressList;
    private LayoutInflater mLayoutInflater;

    private List<AreaInfo> mAreaList = new ArrayList<AreaInfo>();

    private OnClickListener mOnClickListener;

    public AddressSelectAdapter(Activity activity, List<UserAddressInfo> addressList) {
        mActivity = activity;
        mLayoutInflater = LayoutInflater.from(mActivity);
        if (this.addressList == null) {
            this.addressList = new ArrayList<UserAddressInfo>();
        }
        this.addressList.clear();
        if (addressList != null && addressList.size() > 0) {
            this.addressList.addAll(addressList);
        }
    }

    public void setItems(List<UserAddressInfo> addressList) {
        if (this.addressList == null) {
            this.addressList = new ArrayList<UserAddressInfo>();
        }
        this.addressList.clear();
        if (addressList != null && addressList.size() > 0) {
            this.addressList.addAll(addressList);
        }
        notifyDataSetChanged();
    }

    public void setAreaList(List<AreaInfo> areaList) {
        mAreaList = areaList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        int count = this.addressList != null ? this.addressList.size() : 0;
        return count;
    }

    @Override
    public UserAddressInfo getItem(int i) {
        if (i < 0 || i >= this.addressList.size()) {
            return null;
        }
        return this.addressList.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder = null;
        if (view == null) {
            view = mLayoutInflater.inflate(R.layout.view_item_address_select, null, false);
            holder = new ViewHolder();
            holder.txtvName = (TextView) view.findViewById(R.id.txtvName4ItemAddressSelect);
            holder.txtvMobile = (TextView) view.findViewById(R.id.txtvMobile4ItemAddressSelect);
            holder.txtvAddress = (TextView) view.findViewById(R.id.txtvAddress4ItemAddressSelect);
            holder.btnEdit = (ImageView) view.findViewById(R.id.btnEdit4ItemAddressSelect);
            holder.imgvSelected = (ImageView) view.findViewById(R.id.imgvSelected4ItemAddressSelect);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        UserAddressInfo addressInfo = this.addressList.get(position);
        if (addressInfo != null) {
            holder.txtvName.setText(addressInfo.getName());
            holder.txtvMobile.setText(addressInfo.getContactway());

            String province = "";
            String city = "";
            if (!TextUtils.isEmpty(addressInfo.getProvinceid())
                    && !TextUtils.isEmpty(addressInfo.getCityid())) {
                for (AreaInfo provinceInfo : mAreaList) {
                    if (provinceInfo.getId().equals(addressInfo.getProvinceid())) {
                        province = provinceInfo.getName();
                        for (AreaInfo cityInfo : provinceInfo.getNexts()) {
                            if (cityInfo.getId().equals(addressInfo.getCityid())) {
                                city = cityInfo.getName();
                            }
                        }
                    }
                }
            }
            holder.txtvAddress.setText(province + city + addressInfo.getAddress());

            if (addressInfo.getIsdefault() != null && addressInfo.getIsdefault().equalsIgnoreCase("1")) {
                holder.imgvSelected.setVisibility(View.VISIBLE);
            } else {
                holder.imgvSelected.setVisibility(View.INVISIBLE);
            }
            holder.btnEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnClickListener != null) {
                        mOnClickListener.onEditClick(position);
                    }
                }
            });
        }
        return view;
    }

    static class ViewHolder {
        TextView txtvName;
        TextView txtvMobile;
        TextView txtvAddress;
        ImageView btnEdit;
        ImageView imgvSelected;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.mOnClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onEditClick(int position);
    }
}
